package com.rw.coco.data.data.mapper;

import com.rw.coco.api.data.model.CharacterPropertyModel;
import com.rw.coco.api.data.persistence.simple.SimpleCharacterPropertyMapper;
import com.rw.coco.data.data.bean.FileCharacterPropertyModel;
import com.rw.coco.data.data.util.BeanUtil;
import com.rw.tool.util.io.FileIOUtil;
import com.rw.tool.util.io.file.BeanFile;
import com.rw.tool.util.json.JsonUtil;

import java.io.File;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class FileCharacterPropertyMapper extends SimpleCharacterPropertyMapper {
    private static final String CHARACTER_PROPERTY_PATH = "\\property\\";

    private final BeanFile<FileCharacterPropertyModel> beanFile;

    public FileCharacterPropertyMapper(String workPath) {
        File file = new File(workPath, CHARACTER_PROPERTY_PATH);
        FileIOUtil.mkDirs(file);
        this.beanFile = new BeanFile<>(file, FileCharacterPropertyModel.class);
    }

    private FileCharacterPropertyModel convert(CharacterPropertyModel data) {
        return JsonUtil.convertObj(data, FileCharacterPropertyModel.class);
    }

    private CharacterPropertyModel reconvert(FileCharacterPropertyModel data) {
        return JsonUtil.convertObj(data, CharacterPropertyModel.class);
    }

    private FileCharacterPropertyModel removeId(FileCharacterPropertyModel data) {
        return BeanUtil.removeIdProp(data, FileCharacterPropertyModel.class);
    }

    @Override
    public void insert(CharacterPropertyModel data) {
        beanFile.insert(convert(data));
    }

    @Override
    public void insertAll(Collection<CharacterPropertyModel> dataList) {
        List<FileCharacterPropertyModel> modelList = dataList.stream()
                .map(this::convert)
                .collect(Collectors.toList());

        beanFile.insertAll(modelList);
    }

    @Override
    public void delete(Serializable id) {
        beanFile.deleteById(id);
    }

    @Override
    public void deleteAll(CharacterPropertyModel data) {
        beanFile.delete(BeanUtil.getQueryWrapper(data));
    }

    @Override
    public void update(CharacterPropertyModel data) {
        FileCharacterPropertyModel characterPropertyModel = convert(data);
        beanFile.update(BeanUtil.getIdWrapper(characterPropertyModel), removeId(characterPropertyModel));
    }

    @Override
    public CharacterPropertyModel selectOne(Serializable id) {
        return beanFile.selectById(id);
    }

    @Override
    public List<CharacterPropertyModel> selectList(CharacterPropertyModel data) {
        return beanFile.selectList(BeanUtil.getQueryWrapper(data))
                .stream()
                .map(this::reconvert)
                .collect(Collectors.toList());
    }

    @Override
    public List<CharacterPropertyModel> selectList(Collection<? extends Serializable> id) {
        return beanFile.selectList(id)
                .stream()
                .map(this::reconvert)
                .collect(Collectors.toList());
    }
}